//
//  LPTHoriScrollView.m
//  LPTClient
//
//  Created by mac on 2018/12/5.
//  Copyright © 2018年 zhaodd. All rights reserved.
//

#import "LPTHoriScrollView.h"

@interface LPTHoriScrollView ()

@property (nonatomic,strong)UIImageView *indicator;
@property (nonatomic,copy)NSArray *titleArray;
@property (nonatomic,strong)NSMutableArray *subViewArray;
@property (nonatomic,copy) void (^clickBlock) (NSInteger index);

@end

@implementation LPTHoriScrollView

-(instancetype)initWithTitleArray:(NSArray *)array frame:(CGRect)frame selectBlock:(void (^) (NSInteger index))clickBlock 
{
    if (self = [super initWithFrame:frame]) {
        
        _titleArray = array;
        _clickBlock = clickBlock;
        [self initSubViews];
    }
    
    return self;
}

-(void)initSubViews
{
    NSMutableArray *subViewArray = [NSMutableArray arrayWithCapacity:self.titleArray.count];
    self.subViewArray = subViewArray;
    
    for (int i = 0; i < self.titleArray.count; i ++) {
        [UIColor colorWithHexString:@""];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i + 200;
        [button setTitleColor:[UIColor colorWithHexString:@"#FF9000"] forState:UIControlStateSelected];
        [self.subViewArray addObject:button];
    }
    
    UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:self.subViewArray];
    stackView.distribution = UIStackViewDistributionFillEqually;
    stackView.spacing = 0;
    [self addSubview:stackView];
    [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.top.trailing.mas_equalTo(self);
        make.height.mas_equalTo(@(44));
    }];
    
    //indicator
    UIImageView *indicator = [[UIImageView alloc] init];
    indicator.backgroundColor = [UIColor colorWithHexString:@"#FF9000"];
    [self addSubview:indicator];
    self.indicator = indicator;
    [self.indicator mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.bottom.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth/self.titleArray.count, 2));
    }];
    
    UIImageView *line = [[UIImageView alloc] init];
    line.backgroundColor = kThemeGrayColor;
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.trailing.bottom.mas_equalTo(self);
        make.height.mas_equalTo(0.5f);
    }];
}

-(void)clickAction:(UIButton *)button
{
    for (UIButton *otherBtn in self.subViewArray) {
        otherBtn.selected = NO;
    }
    
    button.selected = YES;
    
    NSInteger index = button.tag - 200;
    
    [UIView animateWithDuration:0.2f animations:^{
        
        [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.leading.mas_equalTo(self).mas_offset(kScreenWidth/(self.titleArray.count) * index);
        }];
        
        [self layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        
    }];
    
    if (self.clickBlock) {
        self.clickBlock(index);
    }
}


@end
